package com.qcby.finance.controller;


import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.ObjectUtil;
import com.qcby.finance.base.entity.dto.SpuDTO;
import com.qcby.finance.base.service.SpuService;
import com.qcby.finance.entity.param.SpuParam;
import com.qcby.finance.entity.vo.SpuVO;
import com.qcby.framework.common.exception.ServiceException;
import com.qcby.framework.common.pojo.PageParam;
import com.qcby.framework.common.pojo.PageResult;
import com.qcby.framework.common.pojo.Result;
import org.springframework.web.bind.annotation.*;

import org.springframework.stereotype.Controller;

import javax.annotation.Resource;
import javax.validation.Valid;
import javax.validation.constraints.NotNull;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author 劝书
 * @since 2023-02-19
 */
@RestController
@RequestMapping("/spu")
public class SpuController {

    @Resource
    private SpuService spuService;

    /**
     * spu分页展示，15条一页
     *
     * @param spuParam  spu参数
     * @param pageParam 页面参数
     * @return {@link Result}<{@link PageResult}<{@link SpuVO}>>
     */
    @GetMapping("/show-spu")
    public Result<PageResult<SpuVO>> showSpu(SpuParam spuParam, PageParam pageParam){
        SpuDTO spuDTO = BeanUtil.copyProperties(spuParam, SpuDTO.class);
        Result<PageResult<SpuDTO>> pageResultResult = spuService.showSpu(spuDTO, pageParam);
        List<SpuVO> spuVOs = BeanUtil.copyToList(pageResultResult.getData().getList(), SpuVO.class);
        PageResult<SpuVO> spuVOPageResult = new PageResult();
        spuVOPageResult.setTotal(pageResultResult.getData().getTotal());
        spuVOPageResult.setList(spuVOs);
        return Result.getSuccessResult(spuVOPageResult);
    }

    /**
     * 分组展示spu，15条一页
     *
     * @param spuParam  spu参数
     * @param pageParam 页面参数
     * @return {@link Result}<{@link PageResult}<{@link SpuVO}>>
     */
    @GetMapping("/show-group-spu")
    public Result<PageResult<SpuVO>> showGroupSpu(SpuParam spuParam, PageParam pageParam){
        SpuDTO spuDTO = BeanUtil.copyProperties(spuParam, SpuDTO.class);
        Result<PageResult<SpuDTO>> pageResultResult = spuService.showGroupSpu(spuDTO, pageParam);
        List<SpuVO> spuVOs = BeanUtil.copyToList(pageResultResult.getData().getList(), SpuVO.class);
        PageResult<SpuVO> spuVOPageResult = new PageResult();
        spuVOPageResult.setTotal(pageResultResult.getData().getTotal());
        spuVOPageResult.setList(spuVOs);
        return Result.getSuccessResult(spuVOPageResult);
    }

    /**
     * 添加spu
     *
     * @param spuParam spu参数
     * @return {@link Result}<{@link Boolean}>
     */
    @PostMapping("/add-spu")
    public Result<Boolean> addSpu(@RequestBody SpuParam spuParam){
//        if (ObjectUtil.isNull(spuParam.getId())){
//            throw new ServiceException("500","id不能为空");
//        }
        SpuDTO spuDTO = BeanUtil.copyProperties(spuParam, SpuDTO.class);
        int add = spuService.addSpu(spuDTO);
        return Result.getSuccessResult(add>0);
    }

    /**
     * 编辑spu
     *
     * @param spuParam spu参数
     * @return {@link Result}<{@link Boolean}>
     */
    @PostMapping("/edit-spu")
    public Result<Boolean> editSpu(@RequestBody SpuParam spuParam){
        if (ObjectUtil.isNull(spuParam.getSpuNo())){
            throw new ServiceException("500","spu编号不能为空");
        }
        SpuDTO spuDTO = BeanUtil.copyProperties(spuParam, SpuDTO.class);
        spuService.editSpu(spuDTO);
        return Result.getSuccessResult(Boolean.TRUE);
    }

    @GetMapping("delete-spu")
    public Result<Boolean> deleteSpu(@Valid @NotNull Long id){
        return Result.getSuccessResult(spuService.deleteSpu(id));
    }

}

